package com.itistimetothink.networkdata;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.itistimetothink.cjetso.R;
import com.itistimetothink.cjetso.graphics.IconMarker;
import com.itistimetothink.cjetso.utilities.Marker;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;


public class MyNetworkDataSource extends NetworkDataSource {
	//private static final String BASE_URL = "http://cjetso.itistimetothink.com";
	private static final String BASE_URL = "http://appdata.cjetso.com/get_info.php";

	private static Bitmap icon = null;
	private static Bitmap iconHealth, iconCafe_and_club,iconRestaurant,iconSnack,iconFashion,iconeElectronic,
	iconBeauty,iconBook,iconAccessory,iconToy,iconEvent, iconJetso=null;
	private static final String typeCafeAndClub = "cafe_and_club";
	private static final String typeRestaurant = "restaurant";
	private static final String typeSnack = "snack";
	private static final String typeFashion = "fashion";	
	private static final String typeElectronic = "electronic";
	private static final String typeBeauty = "beauty";
	private static final String typeHealth = "health";
	private static final String typeBook = "book";
	private static final String typeAccessory = "accessory";
	private static final String typeToy = "Toy";
	private static final String typeEvent = "event";
	
	public MyNetworkDataSource(Resources res) {    
		/**
		 * 1 = restaurant
		 * 2 = leisure
		 * 3 = shop
		 * 4 = event
		 */
	    if (res==null) throw new NullPointerException();
        
        createIcon(res);
    }

    protected void createIcon(Resources res) {
        if (res==null) throw new NullPointerException();
        
        //icon=BitmapFactory.decodeResource(res, R.drawable.wikipedia);
        iconCafe_and_club=BitmapFactory.decodeResource(res, R.drawable.cafe_and_club);
        iconRestaurant=BitmapFactory.decodeResource(res, R.drawable.restaurant);
        iconSnack=BitmapFactory.decodeResource(res, R.drawable.snack);
        iconFashion=BitmapFactory.decodeResource(res, R.drawable.fashion);
        iconeElectronic=BitmapFactory.decodeResource(res, R.drawable.electronic);
        iconBeauty=BitmapFactory.decodeResource(res, R.drawable.beauty);
        iconHealth=BitmapFactory.decodeResource(res, R.drawable.health);
        iconBook=BitmapFactory.decodeResource(res, R.drawable.book);
        iconAccessory=BitmapFactory.decodeResource(res, R.drawable.accessory);
        iconToy=BitmapFactory.decodeResource(res, R.drawable.toy);
        iconEvent=BitmapFactory.decodeResource(res, R.drawable.event);
        iconJetso=BitmapFactory.decodeResource(res, R.drawable.jetso);
    }

	@Override
	public String createRequestURL(double lat, double lon) {
		//Log.i("MyNetworkDataSource", BASE_URL+
		//        "?lat=" + lat +
		//        "&lng=" + lon);
		return BASE_URL+
        "?lat=" + lat +
        "&lng=" + lon;
	}

	@Override
	public List<Marker> parse(JSONArray root) {
		if (root==null) return null;
		
		JSONArray jsonArray = null;
		JSONObject jsonObject = null;
		jsonArray=root;
    	List<Marker> markers=new ArrayList<Marker>();
    	
		try {
			if (root.length()==0) return markers;
				int top = Math.min(MAX, root.length());
				for (int i = 0; i < top; i++) {					
					jsonObject = jsonArray.getJSONObject(i);
					Marker ma = processJSONObject(jsonObject);
					if(ma!=null) markers.add(ma);
				}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return markers;
	}
	
	private Marker processJSONObject(JSONObject jo) {
		if (jo==null) return null;
		int color = Color.WHITE;
        Marker ma = null;
        if (	jo.has("name") && 
        		jo.has("lat") && 
        		jo.has("lng") && 
        		jo.has("phone")&&
        		jo.has("address")&&
        		jo.has("description")&&
        		jo.has("type")
        ) {
        	try {
        		if(jo.getString("type").equalsIgnoreCase(typeCafeAndClub)){
        			icon = iconCafe_and_club;
        		}else if(jo.getString("type").equalsIgnoreCase(typeRestaurant)){
        			icon = iconRestaurant;
        		}else if(jo.getString("type").equalsIgnoreCase(typeSnack)){
        			icon = iconSnack;
        		}else if(jo.getString("type").equalsIgnoreCase(typeFashion)){
        			icon = iconFashion;
        		}else if(jo.getString("type").equalsIgnoreCase(typeElectronic)){
        			icon = iconeElectronic;
        		}else if(jo.getString("type").equalsIgnoreCase(typeBeauty)){
        			icon = iconBeauty;
        		}else if(jo.getString("type").equalsIgnoreCase(typeHealth)){
        			icon = iconHealth;
        		}else if(jo.getString("type").equalsIgnoreCase(typeEvent)){
        			icon = iconEvent;
        		}else if(jo.getString("type").equalsIgnoreCase(typeBook)){
        			icon = iconBook;
        		}else if(jo.getString("type").equalsIgnoreCase(typeAccessory)){
        			icon = iconAccessory;
        		}else if(jo.getString("type").equalsIgnoreCase(typeToy)){
        			icon = iconToy;
        		}else{
        			icon = iconEvent;
        		}
        		if(jo.getInt("has_coupon")==1){
        			icon = iconJetso;
        			color = Color.RED;
        		}
        		
        		ma = new IconMarker(
        				jo.getString("name"),
        				jo.getString("address"),
        				jo.getString("phone"),
        				jo.getString("description"),
        				jo.getDouble("lat"),
        				jo.getDouble("lng"),
        				jo.getString("type"),
        				jo.getString("category"),
        				jo.getInt("has_coupon"),
        				jo.getString("coupon"),
        				jo.getString("coupon_pic"),
        				jo.getString("due_date"),
        				jo.getString("pic_1"),
        				jo.getString("pic_2"),
        				jo.getString("pic_3"),
        				jo.getString("like_count"),
        				jo.getString("dislike_count"),
        				jo.getString("not_here"),
        				color,
        				icon);
        		
        		if(jo.has("pic1")){
            		ma.setPicture1(jo.getString("pic1"));
        		}
        		if(jo.has("pic2")){
            		ma.setPicture2(jo.getString("pic2"));
        		}
        		if(jo.has("pic3")){
            		ma.setPicture3(jo.getString("pic3"));
            	}
        		if(jo.has("coupon")){
        			ma.setCoupon(jo.getString("coupon"));
        		}
        		
        		
        	} catch (JSONException e) {
        		e.printStackTrace();
        	}
        }
        return ma;
	}
}